<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Gestalt Sample: Simple Transforms - Python</title>

    <script src="../../js/jquery.js" type="text/javascript"></script>

    <script src="../../js/gestalt.js" type="text/javascript"></script>

</head>
<body >

<script language="python">

# imports
from System import * # for Convert
from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
from System.Windows.Media.Animation import * # exposes Storyboard
#from System.Windows.Media.Imaging import * # for bitmap
from System.Diagnostics import * # enables outputing to a debug window!

# localize
layoutRoot = me.LayoutRoot
tiles = me.tiles

# locals
mouse = Point(0, 0)

#
# define class that procedurally creates Tile code instead of XAML
#
class Tile(Canvas): # inherits from Canvas
    def __init__(self):
    
        ### CREATE INSTANCE VARIABLE ###
        self.targetAngle = 0
        layoutRoot = Canvas()
        self.Children.Add(layoutRoot)
        
        self.bkg = Canvas()
        self.bkg.Width = 30
        self.bkg.Height = 10
        self.bkg.Background = SolidColorBrush(Colors.White)
        self.bkg.RenderTransformOrigin = Point (0.5,0.5)
        Canvas.SetLeft(self.bkg, -15)
        Canvas.SetTop (self.bkg, -5)
        layoutRoot.Children.Add(self.bkg)

        self.scale = ScaleTransform()
        self.scale.ScaleX = 1.0
        self.scale.ScaleY = 1.0
        
        self.rotation = RotateTransform()
        self.rotation.Angle = 0
       
        transformGroup = TransformGroup()
        transformGroup.Children.Add(self.scale)
        transformGroup.Children.Add(self.rotation)
        self.bkg.RenderTransform = transformGroup
        
        dc = DoubleCollection();
        dc.Add(1);
        dc.Add(2);
        
        ellipse = Ellipse()
        ellipse.Stroke = SolidColorBrush(Color.FromArgb(0xFF,0xFF,0x00,0xFF))
        ellipse.StrokeDashArray = dc
            
        ellipse.Width = 10
        ellipse.Height = 10
        Canvas.SetLeft(ellipse, -5)
        Canvas.SetTop (ellipse, -5)
        layoutRoot.Children.Add(ellipse)
                   
        def sb_Completed(sender, e):
            self.rotation.Angle = self.rotation.Angle + ( self.targetAngle - self.rotation.Angle)*.08
            sb.Begin()
        
        sb = Storyboard()
        sb.Completed += sb_Completed
        layoutRoot.Resources.Add("sb",sb)
        sb.Begin()
        
    ### X property ###
    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)
    
    ### Y property ###  
    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)
    
    ### Background property ###  
    def SetBackground(self, value):
        self.bkg.Background = value
    def GetBackground(self):
        return self.bkg.Background
    background = property(GetBackground, SetBackground)

#
# Define event handlers before they are referenced below
#
def Page_MouseMove(sender, e):
    global mouse
    global layoutRoot
    mouse = e.GetPosition(layoutRoot)
    
def sb_Completed(sender, e):
    global tiles
    global sb
    global mouse
    for s in tiles.Children:
        _y = s.Y - mouse.Y
        _x = s.X - mouse.X
        distance = Math.Sqrt((_y * _y) + (_x * _x))
        rad = Math.Atan2(_y, _x)
        angle = rad * (180/Math.PI)
        s.scale.ScaleX = .6 + ((distance / 300.0))
        s.scale.ScaleY = s.scale.ScaleX
        if distance < 200:
            s.targetAngle = angle - 180
        else: 
            s.targetAngle = 0
    sb.Begin()    

#
# attach event handler
#
layoutRoot.MouseMove += Page_MouseMove

#
# run it
#
for x in range(0,15):
    for y in range(0, 10):
        s = Tile()
        s.background = SolidColorBrush(Color.FromArgb(0xcc,Convert.ToByte((100 + (x*10))),0,Convert.ToByte((100 + (y*15)))))
        s.X = 115 + (x * 25)
        s.Y = 25 + (y * 25)
        tiles.Children.Add(s)

sb = Storyboard()
sb.Completed += sb_Completed
layoutRoot.Resources.Add("sb", sb)
sb.Begin()

</script>

    <!-- This isn't a part of the sample, but a link back to the Gestalt site -->
    <div id="upsell" style="color:#fff; font-size: 13px; font-family:'Lucida Grande','Lucida Sans Unicode',Helvetica,Arial,Lucida,Verdana,sans-serif; margin-bottom: 30px;">
        <a href="http://www.visitmix.com/labs/gestalt/samples/" style="background-color: #000; color: #fff; display: block; text-decoration: none;" onmouseover="this.style.color='#000'; this.style.background='#ccc'" onmouseout="this.style.color='#fff'; this.style.background='#000'">
		    <span style="display:block; padding: 8px; text-align:center;">&laquo; Check out other samples on the Gestalt web site</span>
	    </a>
    </div>

    <!-- If you link to a XAML file, you'll be able to view the page correctly only through a server 
         (e.g. apache or IIS) because of security sandbox reasons. To enable running the file locally 
         place the XAML inline directly inside the xml element -->
    <!--<xml src="page.xaml" id="page" class="xaml" width="600" height="300" style="display:none;"></xml>-->
    
    <xml id="page" class="xaml" width="600" height="300" style="display:none;">
        <UserControl 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            Width="600" Height="300">
          <Canvas x:Name="LayoutRoot" Background="#222222">
            <Canvas x:Name="tiles"></Canvas>
          </Canvas>
        </UserControl>
    </xml>
    
</body>
</html>
